<?php

/**
 * 
 * @author NutMotion <simonotm.linux@gmail.com> 
 */
require_once(dirname(__FILE__) . "/../config/credentials.php");
require_once(dirname(__FILE__) . "/../commons/common-funcs.php");
require_once(dirname(__FILE__) . "/../db/globals.php");

function connect() {
    $conn = mysql_pconnect(HOSTNAME, USER, PASSWORD);

    if (!$conn) {
        showError(__FILE__, __FUNCTION__, mysql_error());
        exit();
    } else {
        if (!mysql_select_db(DATABASE, $conn))
            showError(__FILE__, __FUNCTION__, mysql_error($conn));

        $GLOBALS['conn'] = $conn;
    }
}

//
//function queryTable($table, $fields, $conditions) {
//    $arAllResults = array();
//    $query = "SELECT $fields FROM $table"; //WHERE $conditions";
//
//    if (!$result = mysql_query($query, $GLOBALS['conn'])) {
//        showError(__FILE__, __FUNCTION__, mysql_error($GLOBALS['conn']));
//        exit();
//    }
//
//    while ($row = mysql_fetch_array($result)) {
//        $arAllResults[] = $row;
//    }
//
//    return $arAllResults;
//}

function selectFromTable($table, $fields, $conditions) {
    $arAllResults = array();
    $query = "SELECT $fields FROM $table ";

    if ($conditions != null) {
        $query.=" WHERE $conditions";
    }

    if (!$result = mysql_query($query, $GLOBALS['conn'])) {
        showError(__FILE__, __FUNCTION__, $query . " _____________ " . mysql_error($GLOBALS['conn']));
        exit();
    }

    while ($row = mysql_fetch_assoc($result)) {
        $arAllResults[] = $row;
    }

    return $arAllResults;
}

function selectFromTable2($table, $fields, $conditions, $sortByField) {
    $arAllResults = array();
    $query = "SELECT $fields FROM $table ";

    if ($conditions != null) {
        $query.=" WHERE $conditions";
    }

    
    if ($sortByField != null) {
        $query.=" ORDER BY $sortByField";
    }
    
    if (!$result = mysql_query($query, $GLOBALS['conn'])) {
        showError(__FILE__, __FUNCTION__, $query . " _____________ " . mysql_error($GLOBALS['conn']));
        exit();
    }

    while ($row = mysql_fetch_assoc($result)) {
        $arAllResults[] = $row;
    }

    return $arAllResults;
}
function insertIntoTable($table, $fields, $values) {

    $isSuccessful=false;
    $arReturnParameter=array();
    $arFeedbackMsgs=array();

    $query = "INSERT INTO $table ($fields) VALUES ($values)";

    if (mysql_query($query, $GLOBALS['conn'])) {
        $arReturnParameter['default'] = mysql_insert_id($GLOBALS['conn']);
        $arFeedbackMsgs['default']="Created new bookmark!'";
        $isSuccessful=true;
    } else {
        //there was a problem with the insert
        $arReturnParameter['default'] = mysql_errno($GLOBALS['conn']);
        $arFeedbackMsgs['default']="Couldn't complete the request... Error: ".__FUNCTION__."@".__FILE__." '".mysql_error($GLOBALS['conn'])."'";
        $isSuccessful=false;
        
    }
    
    

    return new ResultContext($isSuccessful, $arReturnParameter, $arFeedbackMsgs);
}


/**
 * Same as insertIntoTable but more proper use of the ResultContext class
 * @param type $table
 * @param type $fields
 * @param type $values
 * @return ResultContext 
 */
function insertIntoTable2($table, $fields, $values) {

    $isSuccessful=false;
    $arReturnParameter=array();
    $arFeedbackMsgs=array();

    $query = "INSERT INTO $table ($fields) VALUES ($values)";

    if (mysql_query($query, $GLOBALS['conn'])) {
        $arReturnParameter['default'] = array("id"=>mysql_insert_id($GLOBALS['conn']));
        $arFeedbackMsgs['default']="Created new bookmark!'";
        $isSuccessful=true;
    } else {
        //there was a problem with the insert
        $arReturnParameter['default'] = array("errorId"=>mysql_errno($GLOBALS['conn']));
        $arFeedbackMsgs['default']="Couldn't complete the request... Error: ".__FUNCTION__."@".__FILE__." '".mysql_error($GLOBALS['conn'])."'";
        $isSuccessful=false;    
    }
    
    return new ResultContext($isSuccessful, $arReturnParameter, $arFeedbackMsgs);
}

function deleteFromTable($table, $conditions) {
    $success = false;
    $query = "DELETE FROM $table WHERE $conditions";

    if (!$result = mysql_query($query, $GLOBALS['conn'])) {
        showError(__FILE__, __FUNCTION__, $query . " _____________ " . mysql_error($GLOBALS['conn']));
        exit();
    }
    else
        $success=true;
    
    return $success;
}
?>
